package com.example.treasure_back.service;

import com.example.treasure_back.config.AliOSSConfig;
import com.example.treasure_back.mapper.MapPointMapper;
import com.example.treasure_back.model.MapPointModel;
import com.example.treasure_back.utils.AliOSSUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;

@Service
public class MapPointService {

    @Autowired
    private MapPointMapper mapPointMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private AliOSSConfig aliOSSConfig;

    public boolean uploadImage(InputStream inputStream, String suffix, String point){
        // 上传图片
        String fileName = RandomStringUtils.randomAlphanumeric(32).toUpperCase();
        String pointPath = aliOSSConfig.POINT_PATH + fileName + suffix;
        boolean b = aliOSSUtils.uploadAvatar(inputStream, pointPath);
        if (b){
            // 字符串分割
            String[] split = point.split(",");
            MapPointModel mapPointModel = new MapPointModel();
            mapPointModel.setImage(aliOSSConfig.PATH+pointPath);
            mapPointModel.setLng(split[0]);
            mapPointModel.setLat(split[1]);
            mapPointModel.setStatus(0);
            mapPointMapper.insert(mapPointModel);
            return true;
        }
        return false;
    }
}
